package com.netistate.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.netistate.dao.LaborDao;
import com.netistate.dao.ProjectDao;
import com.netistate.dao.UserDao;
import com.netistate.pojo.Labor;
import com.netistate.pojo.LaborStat;
import com.netistate.pojo.Project;
import com.netistate.pojo.User;

@Service
@Transactional
public class LaborService {
	
	private LaborDao laborDao;
	private UserDao userDao;
	private ProjectDao projectDao;
	/**
	 * 添加工时
	 * @param l
	 */
	public void save(Labor l) {
		laborDao.save(l);
	}
	/**
	 * 查找全部的工时,默认查找两周内的工时,以工时日期查询
	 * @return
	 */
	public List<Labor> findAllLabor() {
		return laborDao.findAllLaborInOneMonth();
	}
	
	/**
	 * 根据传来的条件去查询工时
	 * @param projectName
	 * @param laborUser
	 * @param state
	 * @param beginTime
	 * @param endTime
	 * @param section
	 * @param isLocale
	 * @return
	 */
	public List<Labor> findLaborByCondition(String projectName,
			String laborUser, String state, String beginTime, String endTime,
			String section, String isLocale) {
		if("所有".equals(projectName)){
			//项目为所有的时候
			if("所有".equals(laborUser)){
				//用户和项目为所有时候，只需判断state,section,islocale
				return laborDao.findLaborByCondition(state, beginTime, endTime, section, isLocale);
			}else{
				//用户为某个id时,项目为所有,只判断uid,和state,section,islocale
				User u =  userDao.findById(Integer.parseInt(laborUser));
				return laborDao.findLaborByCondition(u, state, beginTime, endTime, section, isLocale);
			}
		}else{
			//项目为具体某个id时
			if("所有".equals(laborUser)){
				//用户为所有时候,只判断pid和state,section,islocale
				Project p = projectDao.findById(Integer.parseInt(projectName));
				return laborDao.findLaborByCondition(p, state, beginTime, endTime, section, isLocale);
			}else{
				//用户为某个id时，两个都是id，判断pid  uid state,section,islocale
				User u =  userDao.findById(Integer.parseInt(laborUser));
				Project p = projectDao.findById(Integer.parseInt(projectName));
				return laborDao.findLaborByCondition(p, u, state, beginTime, endTime, section, isLocale);
			}
		}
		//return laborDao.findLaborByCondition(projectName, laborUser, state, beginTime, endTime, section, isLocale);
	}
	
	/**
	 * 在条件查询工时的时候，把传来的uid去找User
	 * @param parseInt
	 * @return
	 */
	public User findUserByUid(int uid) {
		return userDao.findById(uid);
	}
	
	/**
	 * 根据Lid去找工时
	 * @return 
	 */
	public Labor findLaborByLId(int lid) {
		return laborDao.findById(lid);
	}
	
	/**
	 * 删除工时
	 * @param parseInt
	 */
	public void del(int lid) {
		laborDao.del(lid);
	}
	
	/**
	 * 工时统计的内容
	 * @return
	 */
	public List<LaborStat> findAllLaborStat(List<Project> pList) {
		
		List<LaborStat> lsList = new ArrayList<LaborStat>();
		
		for(Project p : pList){
			List<LaborStat> lss = laborDao.findLaborStat(p);
			lsList.addAll(lss);
		}
		return lsList;
	}
	
	/**
	 * 根据内容去查找工时统计的东西
	 * @param projectNameBySection
	 * @param section
	 * @param sectionHours
	 * @param sectionPerson
	 */
	public List<LaborStat> findAllLaborStatBySection(List<Project> pList, String projectNameBySection,
			String section, String sectionHours, String sectionPerson) {
		
		List<LaborStat> lsList = new ArrayList<LaborStat>();
		
		
		if("所有".equals(projectNameBySection)){
			if("所有".equals(section)){
				if("".equals(sectionHours)){
					if("".equals(sectionPerson)){
						for(Project p : pList){
							List<LaborStat> lss = laborDao.findLaborStat(p);
							lsList.addAll(lss);
						}
					}else{
						List<LaborStat> lss = new ArrayList<LaborStat>();
						for(Project p : pList){
							lss = laborDao.findLaborStat(p);
							for(LaborStat l : lss){
								if(l.getCurrentPerson().equals(sectionPerson)){
									lsList.addAll(lss);
								}
							}
						}
					}
				}else{
					if("".equals(sectionPerson)){
						for(Project p : pList){
							List<LaborStat> lss = laborDao.findLaborStat(p);
							for(LaborStat l : lss){
								if(l.getWorkTime().equals(sectionHours)){
									lsList.addAll(lss);
								}
							}
						}
					}else{
						for(Project p : pList){
							List<LaborStat> lss = laborDao.findLaborStat(p);
							for(LaborStat l : lss){
								if(l.getCurrentPerson().equals(sectionPerson) && l.getWorkTime().equals(sectionHours)){
									lsList.addAll(lss);
								}
							}
						}
					}
				}
			}else{
				if("".equals(sectionHours)){
					if("".equals(sectionPerson)){
						for(Project p : pList){
							List<LaborStat> lss = laborDao.findLaborStat(p);
							for(LaborStat l : lss){
								if(l.getSection().equals(section)){
									lsList.addAll(lss);
								}
							}
						}
					}else{
						for(Project p : pList){
							List<LaborStat> lss = laborDao.findLaborStat(p);
							for(LaborStat l : lss){
								if(l.getCurrentPerson().equals(sectionPerson) && l.getSection().equals(section)){
									lsList.addAll(lss);
								}
							}
						}
					}
				}else{
					if("".equals(sectionPerson)){
						for(Project p : pList){
							List<LaborStat> lss = laborDao.findLaborStat(p);
							for(LaborStat l : lss){
								if(l.getWorkTime().equals(sectionHours) && l.getSection().equals(section)){
									lsList.addAll(lss);
								}
							}
						}
					}else{
						for(Project p : pList){
							List<LaborStat> lss = laborDao.findLaborStat(p);
							for(LaborStat l : lss){
								if(l.getCurrentPerson().equals(sectionPerson) && l.getWorkTime().equals(sectionHours) && l.getSection().equals(section)){
									lsList.addAll(lss);
								}
							}
						}
					}
				}
			}
		}else{
			Project pp = projectDao.findById(Integer.parseInt(projectNameBySection));
			if("所有".equals(section)){
				if("".equals(sectionHours)){
					if("".equals(sectionPerson)){
						for(Project p : pList){
							List<LaborStat> lss = laborDao.findLaborStat(p);
							for(LaborStat l : lss){
								if(l.getProjectName().equals(pp.getProjectName())){
									lsList.addAll(lss);
								}
							}
						}
					}else{
						for(Project p : pList){
							List<LaborStat> lss = laborDao.findLaborStat(p);
							for(LaborStat l : lss){
								if(l.getCurrentPerson().equals(sectionPerson) && l.getProjectName().equals(pp.getProjectName())){
									lsList.addAll(lss);
								}
							}
						}
					}
				}else{
					if("".equals(sectionPerson)){
						for(Project p : pList){
							List<LaborStat> lss = laborDao.findLaborStat(p);
							for(LaborStat l : lss){
								if(l.getWorkTime().equals(sectionHours) && l.getProjectName().equals(pp.getProjectName())){
									lsList.addAll(lss);
								}
							}
						}
					}else{
						for(Project p : pList){
							List<LaborStat> lss = laborDao.findLaborStat(p);
							for(LaborStat l : lss){
								if(l.getCurrentPerson().equals(sectionPerson) && l.getProjectName().equals(pp.getProjectName()) && l.getWorkTime().equals(sectionHours)){
									lsList.addAll(lss);
								}
							}
						}
					}
				}
			}else{
				if("".equals(sectionHours)){
					if("".equals(sectionPerson)){
						for(Project p : pList){
							List<LaborStat> lss = laborDao.findLaborStat(p);
							for(LaborStat l : lss){
								if(l.getSection().equals(section) && l.getProjectName().equals(pp.getProjectName())){
									lsList.addAll(lss);
								}
							}
						}
					}else{
						for(Project p : pList){
							List<LaborStat> lss = laborDao.findLaborStat(p);
							for(LaborStat l : lss){
								if(l.getCurrentPerson().equals(sectionPerson) && l.getProjectName().equals(pp.getProjectName()) && l.getSection().equals(section)){
									lsList.addAll(lss);
								}
							}
						}
					}
				}else{
					if("".equals(sectionPerson)){
						for(Project p : pList){
							List<LaborStat> lss = laborDao.findLaborStat(p);
							for(LaborStat l : lss){
								if(l.getWorkTime().equals(sectionHours) && l.getProjectName().equals(pp.getProjectName()) && l.getSection().equals(section)){
									lsList.addAll(lss);
								}
							}
						}
					}else{
						for(Project p : pList){
							List<LaborStat> lss = laborDao.findLaborStat(p);
							for(LaborStat l : lss){
								if(l.getCurrentPerson().equals(sectionPerson) && l.getProjectName().equals(pp.getProjectName()) && l.getWorkTime().equals(sectionHours) && l.getSection().equals(section)){
									lsList.addAll(lss);
								}
							}
						}
					}
				}
			}
		}
		return lsList;
		
	}
	
	//--------------------
	@Autowired
	public void setLaborDao(LaborDao laborDao) {
		this.laborDao = laborDao;
	}
	@Autowired
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	@Autowired
	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}
	
	
	
	
	
	
	


	
	
	

}
